package com.jpyl.jpMedicalTreatment.view.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.helper.DeliveryListModel;
import com.jpyl.jpMedicalTreatment.network.Const;
import com.jpyl.jpMedicalTreatment.util.StringUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页列表适配器
 * Created by 15697 on 2019/1/4.
 */

public class DeliveryAdapter extends RecyclerView.Adapter<DeliveryAdapter.ViewHolder> implements
        View.OnClickListener {
    Context context;
    List<DeliveryListModel.Datas> list;
    private int flag;

    public DeliveryAdapter(Context context, List<DeliveryListModel.Datas> list, int flag) {
        this.context = context;
        this.list = list;
        this.flag = flag;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                        .activity_item_delivery,
                null);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        DeliveryListModel.Datas bean = list.get(position);
        String status = bean.getStatus();
        switch (flag) {
            //待发货
            case Const.TOSHIPPED:
                holder.tvItemDeliveryConsignee.setText("收件人：" + bean.getCustomer().getUsername());
                if (status.equals("4")) {
                    status = "待发货";
                }
                break;
            //已发货
            case Const.SHIPPED:
                holder.tvItemDeliveryConsignee.setText("发货人：" + bean.getSupplier().getUsername());
                if (status.equals("5")) {
                    status = "已发货";
                }
                break;
        }
        holder.tvItemDeliveryState.setText(context.getString(R.string.delivery_state) +
                status);

        holder.tvItemDeliveryName.setText(bean.getName());
        String price = StringUtil.isEmpty(bean
                .getPrice()) ? "" : bean
                .getPrice();
        holder.tvItemDeliveryPrice.setText(context.getString(R.string.delivery_price) + price
        );
        holder.tvItemDeliveryCount.setText(context.getString(R.string.delivery_count) + bean
                .getCount());
        holder.tvItemDeliveryTotal.setText(context.getString(R.string.delivery_total) + bean
                .getTotal());
        if (!StringUtil.isEmpty(bean.getOrderdate())) {
            String date = StringUtil.getDateToString(Long.parseLong(bean.getOrderdate()),
                    StringUtil.format);
            holder.tvItemDeliveryTime.setText(context.getString(R.string.delivery_time) + date);
        }
        StringUtil.setImageView(context, holder.imgItemDelivery, bean.getUrl(), false);

        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View view) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener mItemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_itemDelivery)
        ImageView imgItemDelivery;
        @BindView(R.id.tv_itemDelivery_name)
        TextView tvItemDeliveryName;
        @BindView(R.id.tv_itemDelivery_consignee)
        TextView tvItemDeliveryConsignee;
        @BindView(R.id.tv_itemDelivery_price)
        TextView tvItemDeliveryPrice;
        @BindView(R.id.tv_itemDelivery_count)
        TextView tvItemDeliveryCount;
        @BindView(R.id.tv_itemDelivery_total)
        TextView tvItemDeliveryTotal;
        @BindView(R.id.tv_itemDelivery_time)
        TextView tvItemDeliveryTime;
        @BindView(R.id.tv_itemDelivery_state)
        TextView tvItemDeliveryState;
        @BindView(R.id.rela_itemDeliver)
        RelativeLayout relaItemDeliver;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
